<?php
//首页显示
namespace newMobile\Controller;
use Think\Controller;
class StoreController extends CommonController {
	public function _initialize() {
		parent::_initialize();
		$id=I('get.id');
		//获取模版基本信息
		if($id){
			$store=M('store');
	    	$store_info=$store->find($id);
	    	$this->assign('store_info',$store_info);
			if($store_info['status']==0){
	    		$this->error('该店铺已关闭');
	    	}
	    	//店铺详情
			$store_template=M('store_template');
	    	$store_template_info=$store_template->where('store_id='.$id)->find();
	    	$this->assign('store_template_info',$store_template_info);
	    	$shop=M('shop');
	    	$count=$shop->where("store_id=$id and ishidden=0")->count();
			$this->assign('shop_count',$count);
			$coupon_goods=M('coupon_goods');
			$count_coupon=$coupon_goods
						->join('yt_shop on yt_shop.id=yt_coupon_goods.goods_id','left')
						->join('yt_coupon_type on yt_coupon_type.coupon_type_id=yt_coupon_goods.coupon_type_id','left')
						->where("yt_coupon_type.store_id=$id and yt_shop.ishidden=0")->count();
			//echo $coupon_goods->_sql();
			$this->assign('count_coupon',$count_coupon);
			//判断收藏
	    	$users_storecollect=M('users_storecollect');
	    	if(session('user_id')){
	    		
				$newstoreinfo['shore_id']=$id;
				$newstoreinfo['user_id']=session('user_id');
				$rsstoreinfo=$users_storecollect->where($newstoreinfo)->select();
				if($rsstoreinfo){
					$rsstoreinfo_status=1;
				}else{
					$rsstoreinfo_status=0;
				}
	   		}else{
		   		$rsstoreinfo_status=0;
	   		}
	    	$this->assign('rsstoreinfo_status',$rsstoreinfo_status);
		}
    	
		
	}
    public function index(){
    	$id=I('get.id');
    	//促销商品
    	$store_class=M('store_class');
    	$shop=M('shop');
    	$store_class_list=$store_class->where('store_id='.$id)->select();
    	foreach($store_class_list as $key=>$value){
			$shop_ids=explode(',',$value['shop_ids']);
			$store_class_list[$key]['shop_list']=array();
			foreach($shop_ids as $k=>$v){
				$shop_info=$shop->field('id,image,title,price,ishidden')->find($v);
				if($shop_info['ishidden']==0){
					$store_class_list[$key]['shop_list'][$k]=$shop_info;
				}
				
			}
			if($store_class_list[$key]['shop_list']==null){
				unset($store_class_list[$key]);
			}
		}
		$this->assign('store_class_list',$store_class_list);
    	
    	//获取模版
    	$store_template=M('store_template');
    	$store_template_info=$store_template->where('store_id='.$id)->find();
    	$template=$store_template_info['template'];
		$this->display('/Store/template/demo/index');
	}
	public function list_store(){
        //分类
		$store_classification=M('store_classification');
		$store_classifications=$store_classification->select();
    	$this->assign('store_classifications',$store_classifications);
    	//区域
    	$store=M('store');
    	$area_list=$store->field('province')->where('province is not null')->group('province')->select();
    	$this->assign('area_list',$area_list);
    	//店铺
    	$where='yt_store.status=1 ';
    	if($_GET['type_id']){
    		$where.=' AND FIND_IN_SET("'.$_GET['type_id'].'",yt_store.store_classification_type)';
    	}
    	if($_GET['area_id']){
    		$where.=' AND yt_store.province='.$_GET['area_id'];
    	}
    	if($_GET['name']){
    		$where.=' AND yt_store.title like "%'.$_GET['name'].'%"';
    	}
    	
    	$store_list=$store
    				->field('yt_store_template.portrait,yt_store_template.logo,yt_store.title,yt_store.id')
    				->join('yt_store_template ON yt_store.id = yt_store_template.store_id','left')
    				->where($where)
    				->limit($Page->firstRow.','.$Page->listRows)
    				->select();
    	$this->assign('page',$show);// 赋值分页输出
    	$this->assign('store_list',$store_list);
    	$this->display();
	}
	public function ajax_getlist_store(){
		$site_city_id= cookie('site_city_id');
		$where='yt_store.status=1 ';
		$last=I('get.last');
    	$amount=I('get.amount');
    	$store=M('store');
    	/* $store_list=$store
    				->field('yt_store_template.portrait,yt_store_template.logo,yt_store.title,yt_store.id,yt_store.store_classification_type')
    				->join('yt_store_template ON yt_store.id = yt_store_template.store_id','left')
    				->where($where)
    				->limit($last,$amount)
    				->select(); */
    	$lng= cookie('baidu_lng');
        $lat= cookie('baidu_lat');
        $store_list = $store->field('*,(ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$lat.'*PI()/180-lat*PI()/180)/2),2)+COS('.$lat.'*PI()/180)*COS(lat*PI()/180)*POW(SIN(('.$lng.'*PI()/180-lng*PI()/180)/2),2)))*1000))/1000
AS
 juli')->where(' status=1 and (city='.$site_city_id.' or quanwang=1)')->limit($last,$amount)->order('juli asc')->select();
		$store_template=M('store_template');
		
    	foreach($store_list as $key=>$value){
    		$store_classification_type=get_store_class($value['store_classification_type']);
			$rs_info=$store_template->where('store_id='.$value['id'])->find();
			$value['juli']=round($value['juli'], 2);
			if($value['juli']){
				$juli='<span class="flag-security" style="float: right;margin-right: 22px;height: 20px;line-height: 20px;background: #000;">距离'.$value['juli'].'km</span>';
			}else{
				$juli='';
			}
			if($value['quanwang']==1){
				$quanwang='<span class="flag-security" style="background: #ffe000;margin-left: 5px;">全网</span>';
				
			}
    		$list='<li>
					<a href="'.U('store/index',array('id'=>$value['id'])).'" title="'.$value['title'].'">

						<div class="store-name">
							<h1>'.$value['title'].'</h1>
							<span class="flag-security">保</span>'.$quanwang.'
							
							<img src="/themes/wnsc_moblie/newMobile/Store/images/right_arrow.png" class="right-arrow" />
								'.$juli.'
						</div>
						<div class="store-item">
							<img alt="" src="'.$rs_info['logo'].'" class="store-logo">
							<div class="store-right">
								<div class="store-detail">
									<div class="left">
										<div class="scope-business" >经营范围：<strong>'.$store_classification_type.'</strong></div>
										<div class="good-rate">好评率：<strong>90%</strong></div>
										<div class="credibility">信誉：
											<img style="vertical-align: middle;width:15px;" src="/themes/wnsc_moblie/newMobile/Store/images/credibility_starts.png" />
											<img style="vertical-align: middle;width:15px;" src="/themes/wnsc_moblie/newMobile/Store/images/credibility_starts.png"  />
											<img style="vertical-align: middle;width:15px;" src="/themes/wnsc_moblie/newMobile/Store/images/credibility_starts.png"  />
											<img style="vertical-align: middle;width:15px;" src="/themes/wnsc_moblie/newMobile/Store/images/credibility_starts.png"  />
											<img style="vertical-align: middle;width:15px;" src="/themes/wnsc_moblie/newMobile/Store/images/credibility_starts.png"  />
										</div>
									</div>

								</div>
							</div>
						</div>
					</a>
				</li>';
    		$res[]=$list;
    	}
    	$this->ajaxReturn($res);
	}
	public function type(){
		$this->display();
	}
	public function ajax_type(){
		$type_id=I('get.shop_id');
		$store_id=I('get.id');
        $daiyan=I('get.daiyan');
		$shop=M('shop');
		$store_type=M('store_type');
		$type_info=$store_type->find($type_id);
		$this->assign('type_info',$type_info);
		//排序
   		$order=I('get.order',0);
   		$this->assign('order',$order);
   		$orders=array(
   			'0' => 'id desc', 
   			'1' => 'sale_num desc', 
   			'2' => 'click_num desc', 
   			'3' => 'price desc', 
   			'4' => 'price asc', 
   		);
   		$last=I('post.last');
    	$amount=I('post.amount');
		if($type_id==0){
		
			$shop_list=$shop->where('ishidden=0 and store_id='.$store_id)->order($orders[$order])->limit($last,$amount)->select();
		}else{
			$store_type=M('store_type');
			//$types=$this->get_types($type_id);
			//$where['store_type_id']=array('in',$types);
           
            $where['_string'] = 'FIND_IN_SET('.$type_id.',store_type_id)';
			$where['ishidden']=0;
		
			$shop_list=$shop->where($where)->order($orders[$order])->limit($last,$amount)->select();
		}
		foreach($shop_list as $key=>$value){
	   		 $res[]['info'] = '<li><div class="item"><div class="pic_box"></div><div class="goods_images"><a href="'.U('shop/index',array('id'=>$value['id'],'type'=>$daiyan)).'"><img src="'.$value['image'].'"></a></div><dl><dt><a href="'.U('shop/index',array('id'=>$value['id'])).'">'.$value['title'].'</a></dt><dd><i>￥'.$value['price'].'</i><em>￥'.$value['oprice'].'</em> </dd><span>已售'.$value['sale_num'].'件</span></dl></div><!--列表页面加减号--></li>';
	    }
	    $this->ajaxReturn($res);
	}
	
	public function get_types($id){
		$store_type=M('store_type');
		$types=$store_type->where('parentid='.$id)->select();
		if($types){
			foreach($types as $key=>$value){
				$son_types=$store_type->where('parentid='.$value['id'])->select();
				if($son_types){
					$ids=$this->get_types($value['id']);
					$id=$id.','.$ids;
				}else{
					$id=$id.','.$value['id'];
				}
				
				
			}
		}
		return $id;
		
	}
}
?>
